package o;

import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.text.TextUtils;
import com.huawei.appgallery.packagemanager.api.bean.ProfileTask;
import com.huawei.hms.aaid.constant.AaidIdConstant;
import com.huawei.secure.android.common.util.HexUtil;
import com.huawei.secure.android.common.util.IOUtil;
import java.io.Closeable;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Proxy;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.concurrent.TimeUnit;
import o.fuc.c;
import o.fus.a;
import o.fuv.b;

public class cot extends AsyncTask<ProfileTask, Void, ProfileTask> {
    private Context ˎ;
    private String ˏ;
    private ProfileTask ॱ = null;

    protected /* synthetic */ Object doInBackground(Object[] objArr) {
        return ˏ((ProfileTask[]) objArr);
    }

    protected /* synthetic */ void onPostExecute(Object obj) {
        ˎ((ProfileTask) obj);
    }

    public cot(Context context) {
        this.ˎ = context;
    }

    private static String ˏ(Context context, String str) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getApplicationContext().getSystemService("connectivity");
        if (connectivityManager != null) {
            try {
                NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
                fur ॱ = fur.ॱ(str);
                if (ॱ != null) {
                    str = ॱ.ˏॱ().ˎ("net", String.valueOf(doo.ˊ(activeNetworkInfo))).ॱ().toString();
                }
            } catch (Exception e) {
                cnu.ˏ.ˊ("ProfileDownloadTask", "addNetInfoToUrl exception:" + e.getMessage());
            }
        }
        return str;
    }

    protected ProfileTask ˏ(ProfileTask... profileTaskArr) {
        ProfileTask profileTask = null;
        if (profileTaskArr == null || profileTaskArr.length != 1) {
            cnu.ˏ.ˊ("ProfileDownloadTask", "error param");
        } else {
            ProfileTask profileTask2 = profileTaskArr[0];
            this.ॱ = profileTask2;
            this.ˏ = profileTask2.packageName;
            cnu.ˏ.ˋ("ProfileDownloadTask", "start download profile:" + this.ˏ);
            long currentTimeMillis = System.currentTimeMillis();
            fuv ˎ = ˎ();
            a aVar = new a();
            try {
                aVar.ˎ(ˏ(this.ˎ, profileTask2.profileUrl));
                aVar.ˏ(new c().ˎ().ˊ()).ॱ("Accept-Encoding", "identity").ॱ("Cache-Control", "no-cache");
                aVar.ˋ(true);
                ftz ˎ2 = ˎ.ˎ(aVar.ॱ());
                cor.ˎ(this.ˎ).ॱ();
                profileTask = ˋ(profileTask2, currentTimeMillis, ˎ2);
                if (!TextUtils.isEmpty(this.ˏ)) {
                    coo.ˎ(this.ˏ);
                }
                ˊ(profileTask);
                cor.ˎ(this.ˎ).ˏ();
            } catch (IllegalArgumentException e) {
                cnu.ˏ.ˊ("ProfileDownloadTask", "profileUrl is illegal");
            }
        }
        return profileTask;
    }

    private void ˊ(ProfileTask profileTask) {
        if (this.ॱ != null) {
            Intent intent = new Intent("AppProfile.DownloadResult");
            intent.putExtra("AppProfile.DownloadResult.Param.PackageName", this.ॱ.packageName);
            intent.putExtra("AppProfile.DownloadResult.Param.VersionCode", this.ॱ.versionCode);
            if (profileTask == null) {
                intent.putExtra("AppProfile.DownloadResult.Param.Result", 0);
                intent.putExtra("AppProfile.DownloadResult.Param.Reason", this.ॱ.ˋ());
            } else {
                intent.putExtra("AppProfile.DownloadResult.Param.Result", 1);
            }
            ll.ˏ(this.ˎ).ˋ(intent);
        }
    }

    private fuv ˎ() {
        b ˎ = dnb.ॱ().ˉ().ˊ(10000, TimeUnit.MILLISECONDS).ˋ(10000, TimeUnit.MILLISECONDS).ˎ(7000, TimeUnit.MILLISECONDS).ॱ(true).ˎ(true);
        Proxy ˋ = doo.ˋ(this.ˎ);
        if (ˋ != null) {
            ˎ.ॱ(ˋ);
        }
        return ˎ.ˋ();
    }

    private ProfileTask ˋ(ProfileTask profileTask, long j, ftz o_ftz) {
        if (profileTask.status == -1) {
            cnu.ˏ.ˋ("ProfileDownloadTask", "profile task cancel:" + profileTask.packageName);
            return null;
        }
        Closeable ॱ = ॱ(profileTask, o_ftz);
        if (ॱ == null) {
            return null;
        }
        if (ˊ(profileTask, (fvb) ॱ, ˎ((fvb) ॱ))) {
            return null;
        }
        InputStream ˎ = ॱ.ᐝ().ˎ();
        if (ˎ == null) {
            return null;
        }
        try {
            MessageDigest instance = MessageDigest.getInstance(AaidIdConstant.SIGNATURE_SHA256);
            byte[] bArr = new byte[10240];
            File ॱ2 = profileTask.ॱ(this.ˎ);
            try {
                if (ˎ(profileTask, ॱ, ˎ, instance, bArr, new FileOutputStream(ॱ2))) {
                    return null;
                }
                String byteArray2HexStr = HexUtil.byteArray2HexStr(instance.digest());
                if (!byteArray2HexStr.equalsIgnoreCase(profileTask.sha256)) {
                    return ˊ(profileTask, ॱ2, byteArray2HexStr);
                }
                ˋ(profileTask, j, ॱ2);
                return profileTask;
            } catch (FileNotFoundException e) {
                IOUtil.closeSecure(ˎ);
                IOUtil.closeSecure(ॱ);
                ˎ(profileTask, "targetFile not found");
                return null;
            }
        } catch (NoSuchAlgorithmException e2) {
            IOUtil.closeSecure(ˎ);
            IOUtil.closeSecure(ॱ);
            ˎ(profileTask, "can not find SHA-256");
            return null;
        }
    }

    private fvb ॱ(ProfileTask profileTask, ftz o_ftz) {
        Throwable th;
        try {
            fvb execute = o_ftz.execute();
            try {
                profileTask.status = 1;
                profileTask.ˏ("");
                return execute;
            } catch (Throwable e) {
                Throwable th2 = e;
                Object obj = execute;
                th = th2;
            }
        } catch (IOException e2) {
            th = e2;
            Closeable closeable = null;
            cnu.ˏ.ˊ("ProfileDownloadTask", "execute call failed", th);
            profileTask.status = -2;
            profileTask.ˏ("execute failed:" + th.toString());
            cor.ˎ(this.ˎ).ˊ(profileTask);
            IOUtil.closeSecure(closeable);
            return null;
        }
    }

    private void ˎ(ProfileTask profileTask, String str) {
        profileTask.status = -2;
        profileTask.ˏ(str);
        cor.ˎ(this.ˎ).ˊ(profileTask);
        cnu.ˏ.ˊ("ProfileDownloadTask", str);
    }

    private long ˎ(fvb o_fvb) {
        int ˊ = o_fvb.ˊ();
        String o_fut = o_fvb.ˎ().toString();
        String o_fur = o_fvb.ॱ().ˏ().toString();
        long ˏ = o_fvb.ᐝ().ˏ();
        cnu.ˏ.ˋ("ProfileDownloadTask", "http response:" + ˊ + "-" + o_fut + "-" + ˏ);
        cnu.ˏ.ˎ("ProfileDownloadTask", "last url:" + o_fur);
        return ˏ;
    }

    private boolean ˊ(ProfileTask profileTask, fvb o_fvb, long j) {
        if (j > 0) {
            return false;
        }
        ˎ(profileTask, "profile size is 0.");
        IOUtil.closeSecure((Closeable) o_fvb);
        return true;
    }

    private boolean ˎ(ProfileTask profileTask, fvb o_fvb, InputStream inputStream, MessageDigest messageDigest, byte[] bArr, FileOutputStream fileOutputStream) {
        boolean z = false;
        while (true) {
            try {
                int read = inputStream.read(bArr);
                if (read == -1) {
                    break;
                }
                fileOutputStream.write(bArr, 0, read);
                messageDigest.update(bArr, 0, read);
            } catch (Throwable e) {
                profileTask.status = -2;
                profileTask.ˏ("download execption:" + e.getMessage());
                cor.ˎ(this.ˎ).ˊ(profileTask);
                cnu.ˏ.ˊ("ProfileDownloadTask", "download execption", e);
                z = true;
            } finally {
                IOUtil.closeSecure((OutputStream) fileOutputStream);
                IOUtil.closeSecure(inputStream);
                IOUtil.closeSecure((Closeable) o_fvb);
            }
        }
        fileOutputStream.flush();
        return z;
    }

    private void ˋ(ProfileTask profileTask, long j, File file) {
        if (!file.renameTo(profileTask.ˋ(this.ˎ))) {
            cnu.ˏ.ˊ("ProfileDownloadTask", "rename failed");
        }
        profileTask.status = 2;
        cor.ˎ(this.ˎ).ˊ(profileTask);
        cnu.ˏ.ˋ("ProfileDownloadTask", "download profile finish,cost:" + (System.currentTimeMillis() - j));
    }

    private ProfileTask ˊ(ProfileTask profileTask, File file, String str) {
        if (!file.delete()) {
            cnu.ˏ.ˊ("ProfileDownloadTask", "delete targetFile failed");
        }
        profileTask.status = -2;
        profileTask.ˏ("file verify failed:" + str);
        cor.ˎ(this.ˎ).ˊ(profileTask);
        cnu.ˏ.ˊ("ProfileDownloadTask", "file verify failed,sha256 unsame:" + str + "-" + profileTask.sha256);
        return null;
    }

    protected void ˎ(ProfileTask profileTask) {
    }
}
